/* ************************************************************************ */
/* PROGRAM: millermdacsurvreg3.sas                    LAST UPDATED:01/24/19 */ 
/* ------------------------------------------------------------------------ */
/* Program conducts a proc surveyreg analysis on the long data, part of the */
/*  research of Sarah Miller of the University of Michigan and Laura Wherry */
/*  of the University of San Diego, MDAC MS#018.                            */
/*  This study will look at the affect of the Affordable Care Act (ACA) on  */
/*  mortality outcomes.  Only 26 states participated in the ACA and         */
/*  the research will compare the results for the 26 states to the non      */
/*  participating states.                                                   */
/* Basic exclusions are implemented to obtain a core MDAC analysis file.    */
/* ************************************************************************ */
LIBNAME mdaclib '/data/mdac/library';
LIBNAME datalib '/data/mdac/data/research_file';
LIBNAME caidlib '/data/mdac/data/cms/cms_data';
LIBNAME millrlib '/data/mdac/data/miller';

*filename outpfile '/data/mdac/data/temp/mdacpuf.dat';

OPTION LS=80 fmtsearch=(mdaclib);

PROC FORMAT;
 value $povpimfmt
              '   ' = 'NIU/Miss'
      '000' - '138' = 'LE138FPL'
      '139' - '399' = '139399FPL'
      '400' - '999' = 'GE400FPL' ;     

 value $povpimfmtx
                '1' = 'LE138FPL'
                '2' = '139399FPL'
                '3' = 'GE400FPL'      
                '9' = 'NIU/Miss' ;

 value follofmt 0 = 'Censored'
                1 = 'dth 2008'
                2 = 'dth 2009'
                3 = 'dth 2010'
                4 = 'dth 2011'
                5 = 'dth 2012'
                6 = 'dth 2013'
                7 = 'dth 2014'
                8 = 'dth 2015' ;

 value follo2fmt 0 = 'Censored'
                 1 = 'dth20081'
                 2 = 'dth20082'
                 3 = 'dth20091'
                 4 = 'dth20092'
                 5 = 'dth20101'
                 6 = 'dth20102'
                 7 = 'dth20111'
                 8 = 'dth20112'
                 9 = 'dth20121'
                10 = 'dth20122'
                11 = 'dth20131'
                12 = 'dth20132'
                13 = 'dth20141'
                14 = 'dth20142'
                15 = 'dth20151'
                16 = 'dth20152'  ;

 value age14fmt 0 - 18 = 'Age 0-18'
               19 - 49 = 'Age19-49'
               50 - 64 = 'Age50-64'
              65 - 200 = 'Age 65+ ' ;
run;

* ************************************************************** *;
* xxx GENERATE LONG FORM OF DATA (INDIV X YR LEV OF OBSERVATION) *;
* ************************************************************** *;

%macro longdatayear;
 %do i=1 %to 8;

 data mdac&i;
  set millrlib.millermdacdbase1;
   period = &i;
   if foloprd = &i then inddied = 1; else inddied = 0;
   if inddied = 1 and indhcamen = 1 then inddiedamen = 1;
                                    else inddiedamen = 0;
   if inddied = 1 and indexternal = 1 then inddiedext = 1;
                                      else inddiedext = 0;
   if inddied = 1 and indinternal = 1 then inddiedint = 1;
                                      else inddiedint = 0;
   if foloprd NE 0 and foloprd LT &i then delete;
 %if &i lt 8 %then %do ;
  enrolled=indeligy&i ;
  nummonths=monthsy&i; 
 %end;
 run;

%end;
%mend;
%longdatayear;


data mdaclong;
 set mdac1-mdac8;
run;

proc contents data=mdaclong; run;
*endsas;

* *********************************************************** *;
* xxx DEFINE VARIABLES INDICATING TIME PASSED SINCE EXPANSION *;
* *********************************************************** *;
 data millrlib.mdaclongxt;
   set mdaclong;

 indevent2 = 0;
 if indtreated14a = 1 and period = 8 then indevent2 = 1;
 if indtreated14b = 1 and period = 8 then indevent2 = 1;

 indevent1 = 0;
 if indtreated14a = 1 and period = 7 then indevent1 = 1;
 if indtreated14b = 1 and period = 7 then indevent1 = 1;
 if indtreated15a = 1 and period = 8 then indevent1 = 1;
 if indtreated15b = 1 and period = 8 then indevent1 = 1;

 indeventm0=0;
 if indtreated14a = 1 and period = 7 then indevent1 = 1;
 if indtreated14b = 1 and period = 7 then indevent1 = 1;
 if indtreated15a = 1 and period = 8 then indevent1 = 1;
 if indtreated15b = 1 and period = 8 then indevent1 = 1;

 indeventm1=0;
 if indtreated14a = 1 and period = 6 then indeventm1 = 1;
 if indtreated14b = 1 and period = 6 then indeventm1 = 1;
 if indtreated15a = 1 and period = 7 then indeventm1 = 1;
 if indtreated15b = 1 and period = 7 then indeventm1 = 1;

 indeventm2=0;
 if indtreated14a = 1 and period = 5 then indeventm2 = 1;
 if indtreated14b = 1 and period = 5 then indeventm2 = 1;
 if indtreated15a = 1 and period = 6 then indeventm2 = 1;
 if indtreated15b = 1 and period = 6 then indeventm2 = 1;

 indeventm3=0;
 if indtreated14a = 1 and period = 4 then indeventm3 = 1;
 if indtreated14b = 1 and period = 4 then indeventm3 = 1;
 if indtreated15a = 1 and period = 5 then indeventm3 = 1;
 if indtreated15b = 1 and period = 5 then indeventm3 = 1;

 indeventm4=0;
 if indtreated14a = 1 and period = 3 then indeventm4 = 1;
 if indtreated14b = 1 and period = 3 then indeventm4 = 1;
 if indtreated15a = 1 and period = 4 then indeventm4 = 1;
 if indtreated15b = 1 and period = 4 then indeventm4 = 1;
 
 indeventm5=0;
 if indtreated14a = 1 and period = 2 then indeventm5 = 1;
 if indtreated14b = 1 and period = 2 then indeventm5 = 1;
 if indtreated15a = 1 and period = 3 then indeventm5 = 1;
 if indtreated15b = 1 and period = 3 then indeventm5 = 1;
 
 indeventm6=0;
 if indtreated14a = 1 and period = 1 then indeventm6 = 1;
 if indtreated14b = 1 and period = 1 then indeventm6 = 1;
 if indtreated15a = 1 and period LT 3 then indeventm6 = 1;
 if indtreated15b = 1 and period LT 3 then indeventm6 = 1;

indpost=0;
if indevent2=1 then indpost=1;
if indevent1=1 then indpost=1;

run;

data mdaclongx;
 set millrlib.mdaclongxt ;
run;
* *************************** *;
* xxx INITIAL ANALYSIS        *;
*          *;
* *************************** *;
* *************************** *;
* xxx PULL TARGET SAMPLE 1a   *;
* *************************** *;
 data millrlib.mdac_target1;
  set mdaclongx;

 indtarget = 0;
 if milpovct = '1' then indtarget = 1;
 if indedulths = 1 then indtarget = 1;

 if indtarget = 0 then delete;
 if indearlyexp = 1 then delete;
 if 20 <= age_2014 <= 64 ; * KEEP AGES 20-64 AT TIME OF END OF FOLLOW-UP ;
 if cit ^= '5' ;
 if ssiposinc gt 0 then delete;
run;

* ************************* *;
* xxx PULL TARGET SAMPLE AGE 55-64 *;
* ************************* *;
data mdac_target1b;
 set millrlib.mdac_target1;
 if 55 <= AGE_2014 <= 64 ;
run;

data mdac_target1bw;
 set mdac_target1w;
 if 55 <= AGE_2014 <= 64 ;
run;

proc freq data=mdac_target1bw;
title1 '**************************************';
title2 '* FREQ: mdac_target1bw               *';
title3 '**************************************';
tables sex age milpovct rac2 hicov dphy ;
run;

proc means data=mdac_target1bw;
title1 '***************************************';
title2 '* MEANS: mdac_target1bw               *';
title3 '***************************************';
weight mdac_wgt;
var indfemale age indblack indwhite indother indhisp 
    
 indunins indedulths indedueqhs indedusomcol 
    indedubamore inddie20082015 indinternal indexternal indhcamen;
run;

proc surveyreg data=mdac_target1b;
title1 '************************************************* *';
title2 '* SURVEYREG: data=mdac_target1b, dep var= inddied *';
title3 '************************************************* *';
 weight mdac_wgt;
 cluster st;
 class st period;

 model inddied = indevent2 indevent1 
     indeventm0 indeventm1 indeventm2 indeventm3
     indeventm4 indeventm5 indeventm6 st period / solution;
run;

proc surveyreg data=mdac_target1b;
title1 '***************************************************** *';
title2 '* SURVEYREG: data=mdac_target1b, dep var= inddiedamen *';
title3 '***************************************************** *';
 weight mdac_wgt;
 cluster st;
 class st period;

 model inddiedamen = indevent2 indevent1 
     indeventm0 indeventm1 indeventm2 indeventm3
     indeventm4 indeventm5 indeventm6 st period / solution;
run;

proc surveyreg data=mdac_target1b;
title1 '**************************************************** *';
title2 '* SURVEYREG: data=mdac_target1b, dep var=inddiedext  *';
title3 '**************************************************** *';
 weight mdac_wgt;
 cluster st;
 class st period;

 model inddiedext = indevent2 indevent1 
     indeventm0 indeventm1 indeventm2 indeventm3
     indeventm4 indeventm5 indeventm6 st period / solution;
run;

proc surveyreg data=mdac_target1b;
title1 '**************************************************** *';
title2 '* SURVEYREG: data=mdac_target1b, dep var=inddiedint  *';
title3 '**************************************************** *';
 weight mdac_wgt;
 cluster st;
 class st period;

 model inddiedint = indevent2 indevent1 
     indeventm0 indeventm1 indeventm2 indeventm3
     indeventm4 indeventm5 indeventm6 st period / solution;
run;



proc surveyreg data=mdac_target1b;
title1 '************************************************* *';
title2 '* SURVEYREG: data=mdac_target1b, dep var= inddied *';
title3 '************************************************* *';
 weight mdac_wgt;
 cluster st;
 class st period;

 model inddied = indpost st period / solution;
run;

proc surveyreg data=mdac_target1b;
title1 '***************************************************** *';
title2 '* SURVEYREG: data=mdac_target1b, dep var= inddiedamen *';
title3 '***************************************************** *';
 weight mdac_wgt;
 cluster st;
 class st period;

 model inddiedamen = indpost  st period / solution;
run;

proc surveyreg data=mdac_target1b;
title1 '**************************************************** *';
title2 '* SURVEYREG: data=mdac_target1b, dep var=inddiedext  *';
title3 '**************************************************** *';
 weight mdac_wgt;
 cluster st;
 class st period;

 model inddiedext = indpost st period / solution;
run;

proc surveyreg data=mdac_target1b;
title1 '**************************************************** *';
title2 '* SURVEYREG: data=mdac_target1b, dep var=inddiedint  *';
title3 '**************************************************** *';
 weight mdac_wgt;
 cluster st;
 class st period;

 model inddiedint = indpost  st period / solution;
run;
